package br.com.unibratec.receiver;

import java.util.Random;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.widget.RemoteViews;
import br.com.unibratec.R;
import br.com.unibratec.negocio.Evento;
import br.com.unibratec.service.WidgetService;
import br.com.unibratec.view.CadastrarEventosActivity;

public class WidgetProvider extends AppWidgetProvider {

	public static final String LOG_TAG = "WIDGET";
	public static final String ACTION_UPDWIGET = "br.com.unibratec.ACTION_UPDWIGET";

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		printLog("WidgetProvider:onReceive" + intent.getAction());
		int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		if (ACTION_UPDWIGET.equals(intent.getAction())) {
			Evento evento = (Evento) intent.getSerializableExtra("evento");
			if (evento != null) {
				printLog("onReceive:evento => " + evento.toString());
				views.setOnClickPendingIntent(R.idWidget.textViewDescricao, this.createPendingIntentActivity(context, evento, CadastrarEventosActivity.class, appWidgetId));
				views.setTextViewText(R.idWidget.textViewDescricao, evento.toString());
				if (evento.isAtivo()) {
					views.setTextColor(R.idWidget.textViewDescricao, Color.BLACK);
				} else {
					views.setTextColor(R.idWidget.textViewDescricao, Color.RED);
				}

			} else {
				views.setTextViewText(R.idWidget.textViewDescricao, "Nenhum evento cadastrado");
			}
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				printLog("onReceive:appWidgetId => " + appWidgetId);
				AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);
			} else {
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
				ComponentName componentName = new ComponentName(context, WidgetProvider.class);
				int[] appWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
				printLog("onReceive:appWidgetIds => " + appWidgetIds);
				this.onUpdate(context, appWidgetManager, appWidgetIds);
			}
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		printLog("onUpdate");

		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		for (int appWidgetId : appWidgetIds) {
			printLog("appWidgetId => " + appWidgetId);
			views.setOnClickPendingIntent(R.idWidget.prev, this.createPendingIntentService(context, WidgetService.ACTION_PREVIOUS, appWidgetId));
			views.setOnClickPendingIntent(R.idWidget.next, this.createPendingIntentService(context, WidgetService.ACTION_NEXT, appWidgetId));
			views.setOnClickPendingIntent(R.idWidget.add, this.createPendingIntentActivity(context, CadastrarEventosActivity.class, appWidgetId));

			Intent it = new Intent(context, WidgetService.class);
			it.setAction(WidgetService.ACTION_LOAD_DATA);
			it.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
			context.startService(it);
		}
		appWidgetManager.updateAppWidget(appWidgetIds, views);
	}

	private Intent createIntentService(Context context, String acao, int appWidgetId) {
		Intent it = new Intent(context, WidgetService.class);
		it.setAction(acao);
		// it.putExtra("acao", acao);
		it.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		return it;
	}

	private PendingIntent createPendingIntentService(Context context, String acao, int appWidgetId) {
		printLog("createPendingIntentService:appWidget =>  " + appWidgetId);
		Intent it = createIntentService(context, acao, appWidgetId);
		return PendingIntent.getService(context, new Random().nextInt(), it, PendingIntent.FLAG_UPDATE_CURRENT);
	}

	private PendingIntent createPendingIntentActivity(Context context, Class<? extends Activity> classe, int appWidgetId) {
		printLog("createPendingIntentActivity =>  " + appWidgetId);
		Intent it = new Intent(context, classe);
		it.setAction("openActivity");
		// it.putExtra("acao", "openActivity");
		it.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		return PendingIntent.getActivity(context, new Random().nextInt(), it, PendingIntent.FLAG_UPDATE_CURRENT);
	}

	private PendingIntent createPendingIntentActivity(Context context, Evento evento, Class<? extends Activity> classe, int appWidgetId) {
		printLog("createPendingIntentService:appWidget =>  " + appWidgetId);
		Intent it = new Intent(context, classe);
		it.setAction("openActivity");
		// it.putExtra("acao", "openActivity");
		it.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		it.putExtra("evento", evento);
		return PendingIntent.getActivity(context, (int) evento.getId(), it, PendingIntent.FLAG_UPDATE_CURRENT);
	}

	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		printLog("onDeleted");
	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		printLog("onEnabled");
	}

	public static void printLog(String descricao) {
		Log.d(LOG_TAG, descricao);
	}
}
